<?php if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );

class Cart extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    function get_user_cart($uid){
    	$sql = "select * from user_cart where uid = " . $uid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_user_maker_address($uid){
    	$sql = "select
			m.id,
			u.uid,
			m.uname,
			p.name as pname,
			c.name as cname,
			d.name as dname,
			m.area,
			m.zip_code,
			m.mobile
			from user_maker_address m, user u, province p, city c, dist d
			where m.uid = u.uid
			and m.prov_code = p.code
			and m.city_code = c.code
			and m.dist_code = d.code
			and m.uid = " . $uid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_user_cart_count($uid){
    	$sql = "select sum(quantity) as quantitys from user_cart where ifnull(quantity,1) <> 0 and uid = " . $uid;
    	$query = $this->db->query($sql);
    	$row = $query->first_row();
    	return $row->quantitys;
    }
    function get_user_cart_info($uid){
    	$sql = "select uc.id,uc.m_id,uc.material_id,m.name as material_name , ROUND(m.price,2) price ,
				m.img as material_img,uc.quantity,um.name as mname,u.uid,u.name as uname
				,(select CONCAT(path,name) from model_picture mp where mp.m_id = uc.m_id and cover = 1) as img
				, ROUND((uc.quantity*m.price),2) as cost
				from user_cart uc, user_model um ,user u , material m
				where u.uid = uc.uid and uc.m_id = um.id and m.id = uc.material_id
				and ifnull(uc.quantity,1) <> 0 and uc.uid = " . $uid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_user_cart_total($uid){
    	$sql = "select ROUND(sum((uc.quantity*m.price)),2) as cost ,
			sum(quantity) as total
			,ROUND((sum(quantity)/2)*15,2) as ship
			from user_cart uc, material m
			where m.id = uc.material_id and ifnull(uc.quantity,1) <> 0 and uc.uid = " . $uid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
}